package hy;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;

public class DisServer {
    String url="192.168.188.100:2181,192.168.188.101:2181";
    int timeout=20000;
    ZooKeeper cli;

    //连接zk集群
    ZooKeeper getConnect()throws Exception{
        return new ZooKeeper(url,timeout,null);
    }

    //注册节点
    void regist(String ip)throws Exception{
        cli.create("/servers/server_"+ip+"_",ip.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println(ip+"服务器上线了");
    }

    public static void main(String[] args)throws Exception {
        DisServer server=new DisServer();
        server.cli=server.getConnect();
        server.regist("192.168.1.148");
        Thread.sleep(Integer.MAX_VALUE);
    }
}
